<?php
namespace Modules\ShopApi\Models;
use Illuminate\Support\Facades\DB;
use Modules\Admin\Services\UserService; 
class ShopProduct extends BaseApiModel
{	
	/**
     * @name  关联图片
     */
    public function image_one()
    {
        return $this->hasOne('Modules\Admin\Models\Image','id','image_id');
    }
	/**
     * @name  关联sku
     */
    public function product_spec_to()
    {
        return $this->hasMany('Modules\ShopApi\Models\ShopProductSpec','product_id','id');
    }
	/**
     * @name  关联商品评价
     */
    public function product_comment_to()
    {
        return $this->hasMany('Modules\ShopApi\Models\ShopProductComment','product_id','id');
    }
	/**
     * @name  商品详情
     */
	public function product_info($id){
		$info = $this->select('id','name','images','attrbute_list','content','specs_status','sales')
				->with([
					'product_spec_to'=>function($query){
						$query->select('id','product_id','money','bazaar_money','postage','stock','sku');
					},
					'product_comment_to'=>function($query){
						$query->select('id','product_id','content','user_id','level','created_at')->orderBy('id','desc')->limit(1);
					},
					'product_comment_to.user_to'=>function($query){
						$query->select('id','nick_name','avatar_url','image_id');
					},
					'product_comment_to.user_to.image_one'=>function($query){
						$query->select('id','url','open');
					}
				])
				->withCount('product_comment_to')
				->where('id',$id)
				->first()
				->toArray();
		$authorization = \Request::header('authorization');	
		if($authorization){
			$user_info = (new UserService())->my();
			if($user_info===false){
				$info['collected'] = false;
				$info['cart_num'] = 0;
			}else if(isset($user_info['code']) && $user_info['code'] == 1003){
				return $this->api_error($user_info);
			}else if(DB::table('shop_collects')->where(['product_id'=>$info['id'],'user_id'=>$user_info['id']])->count()){
				$info['cart_num'] = DB::table('shop_carts')->where(['user_id'=>$user_info['id']])->sum('num');
				$info['collected'] = true;
			}else{
				$info['cart_num'] = DB::table('shop_carts')->where(['user_id'=>$user_info['id']])->sum('num');
				$info['collected'] = false;
			}
		}else{
			$info['collected'] = false;
			$info['cart_num'] = 0;
		}
		$info['product_comment_to'] = $this->product_comment_to_info($info['product_comment_to']);		
		if($info['specs_status'] == 1){
			$info['attrbute_list'] = json_decode($info['attrbute_list'],true);
		}
		$info['content'] = replacePicUrl($info['content']);
		//$info['product_spec_to'] = $this->sku_list($info['product_spec_to']);	
		$info['images'] = $this->images_list($info['images']);	
		return $this->api_success(['data'=>$info]);	
	}
	/**
     * @name  评论数据处理
     */
	 private function product_comment_to_info($product_comment_to){
		 $arr = [];
		 if(count($product_comment_to)>0){
			 $arr['content']= $product_comment_to[0]['content'];
			 $arr['level']= $product_comment_to[0]['level'];
			 $arr['created_at']= $product_comment_to[0]['created_at'];
			 $arr['nick_name']= $product_comment_to[0]['user_to']['nick_name'];
			 $arr['nick_name']= $arr['nick_name']?sub_str($arr['nick_name'],1):'***';
			 if($product_comment_to[0]['user_to']['image_id']){
				if($product_comment_to[0]['user_to']['image_one']['open'] == 1){
					$arr['image_url'] = getHttpType() . $product_comment_to[0]['user_to']['image_one']['url'];
				}else{
					$arr['image_url'] = $product_comment_to[0]['user_to']['image_one']['url'];
				}
			}else{
				$arr['image_url'] = $product_comment_to[0]['user_to']['avatar_url'];
			}
		 }
		 return $arr;
	 }
	/**
     * @name  sku数据处理
     */
	 private function sku_list($sku){
		$product_spec_to = [];
		foreach($sku as $k1=>$v1){
			$product_spec_to[$v1['sku']?$v1['sku']:0] = [
				'money'=>$v1['money'],
				'bazaar_money'=>$v1['bazaar_money'],
				'postage'=>$v1['postage'],
				'stock'=>$v1['stock']
			];
		}
		return $product_spec_to;
	 }
	/**
     * @name  图片处理
     */
	 private function images_list($images){
		 if($images){
			$images = obj_to_arr(DB::table('images')->whereIn('id',explode('|',$images))->select('id','url','open')->get());	
			$http = getHttpType();	 	
			foreach($images as $k=>$v){
				if($v['open'] == 1){
					$images[$k]['url'] = $http . $v['url'];
				}else{
					$images[$k]['url'] = $v['url'];
				}
			}
		}
		return $images;
	 }
	/**
     * @name  商品列表
     */
	public function product_list($data){
		$model = $this->query();
		$model = $model->where('status',1);
		if($data['type'] == 1){
			$model = $model->where('recommend_status',0);
			$model = $model->where('best_status',0);
		}else if($data['type'] == 2){
			$model = $model->where('recommend_status',1);
		}else if($data['type'] == 3){
			$model = $model->where('best_status',1);
		}
		if(isset($data['name']) && $data['name'] != ''){
			$model = $model->where('name','like','%' . $data['name'] . '%');
		}
		if(isset($data['sales'])){
			if($data['sales'] == 1){
				$model = $model->orderBy('sales','desc');
			}
		}
		if(isset($data['new_products']) && $data['new_products'] == 1){
			$model = $model->orderBy('id','desc');
		}
		if(isset($data['size_money'])){
			if($data['size_money'] == 1){
				$model = $model->orderBy('money','asc');
			}else if($data['size_money'] == 2){
				$model = $model->orderBy('money','desc');
			}
		}
		if(isset($data['product_type_id']) && $data['product_type_id']){
			$model = $model->where('product_type_id',$data['product_type_id']);
		}
		$list = $model->select('id','name','image_id','money','sales','bazaar_money')
				->with([
				'image_one'=>function($query){
					$query->select('id','url','open');
				}
				])
				->orderBy('sort','asc')
				->paginate(config('shopapi.per_page_product'))
				->toArray();	
		$list['data'] = $this->image_list($list['data']);
		return $this->api_success(['data'=>[
			'data'			=> $list['data'],
			'last_page'		=> $list['last_page'],
			'per_page'		=> $list['per_page'],
			'current_page'	=> $list['current_page'],
			'total' 		=> $list['total']
		]]);			
	}
	/**
     * @name  首页精品推荐和首页热卖单品的数据列表
	 * @param type 1= 精品推荐 2= 热卖单品
     */
	public function recommend_product_list($type,$page){
		$where = ['status'=>1];
		if($type == 1){
			$where['recommend_status'] = 1; 
		}else if($type == 2){
			$where['best_status'] = 1; 
		}
		if($page){
			$list = $this->select('id','name','image_id','money','sales','bazaar_money')
				->with([
				'image_one'=>function($query){
					$query->select('id','url','open');
				}      
				])
				->orderBy('sort','asc')
				->orderBy('id','desc')
				->where($where)
				->paginate(config('shopapi.per_page_product'))
				->toArray();
				$list['data'] = $this->image_list($list['data']);
				return $this->api_success(['data'=>[
					'data'			=> $list['data'],
					'last_page'		=> $list['last_page'],
					'per_page'		=> $list['per_page'],
					'current_page'	=> $list['current_page'],
					'total' 		=> $list['total']
				]]);
		}else{
			$data = $this->select('id','name','image_id','money','sales','bazaar_money')
				->with([
				'image_one'=>function($query){
					$query->select('id','url','open');
				}      
				])
				->limit(4)
				->orderBy('sort','asc')
				->orderBy('id','desc')
				->where($where)
				->get()
				->toArray();
				$data = $this->image_list($data);
				return $this->api_success(['data'=>$data]);	
		}
		
			
	}
	
}
